package test;

import Jama.Matrix;
import textmining.gpanalysis.AnalyzerUtil;
import textmining.gpanalysis.KEUFKeywordVectorGenerator;
import data.KeywordVector;
import textmining.gpanalysis.KeywordVectorGenerator;
import textmining.gpanalysis.TFIDFKeywordVectorGenerator;
import textmining.gpanalysis.VectorXmlBuilder;
import data.Post;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;


/**
 *
 * @author ibrahimsabek
 */
public class SimilarityMain {

     public static void main(String[] args) throws FileNotFoundException, IOException {

       //posts
        Post post1 = fillPostFromFile("txtInput/text1.txt", 1,"title1", "cat1", new String[]{"tag11","tag12"});
        Post post2 = fillPostFromFile("txtInput/text2.txt", 2,"title2", "cat2", new String[]{"tag21","tag22"});
        Post post3 = fillPostFromFile("txtInput/text3.txt", 3,"title3", "cat3", new String[]{"tag31","tag32"});
        Post post4 = fillPostFromFile("txtInput/text4.txt", 4,"title4", "cat4", new String[]{"tag41","tag42"});
        Post post5 = fillPostFromFile("txtInput/text5.txt", 5,"title5", "cat5", new String[]{"tag51","tag52"});
        Post post6 = fillPostFromFile("txtInput/text6.txt", 6,"title6", "cat6", new String[]{"tag61","tag62"});
        Post post7 = fillPostFromFile("txtInput/text7.txt", 7,"title7", "cat7", new String[]{"tag71","tag72"});
        Post post8 = fillPostFromFile("txtInput/text8.txt", 8,"title8", "cat8", new String[]{"tag81","tag82"});
         /*
         KeywordVectorGenerator keywordVectorGenerator = new KEUFKeywordVectorGenerator(post1);
         HashMap<String,Double> keywordVector = (HashMap<String,Double>)keywordVectorGenerator.generateVector();
         for(String key : keywordVector.keySet()){
            System.out.println(key +" : "+keywordVector.get(key));
         }
        */

         KeywordVectorGenerator keywordVectorGenerator = new TFIDFKeywordVectorGenerator(post8);
         keywordVectorGenerator.setPosts(new Post[]{post2, post3, post4, post5, post6, post7});
/*
         Matrix TFMatrix = (Matrix)keywordVectorGenerator.generateKeywordsContainer();
         System.out.println("TF matrix");
         TFMatrix.print(1, 1);

         Matrix IDFMatrix = ((TFIDFKeywordVectorGenerator)keywordVectorGenerator).createTFIDFMatrix(TFMatrix);
         System.out.println("IDF matrix");
         IDFMatrix.print(1, 4);
         */

         VectorXmlBuilder vectorXmlBuilder = new VectorXmlBuilder();
         KeywordVector[] vectors = ((TFIDFKeywordVectorGenerator)keywordVectorGenerator).generateKeywordsVectors();
         for(KeywordVector vector : vectors){
             vectorXmlBuilder.createVectorXml(vector);
         }

     }

     private static Post fillPostFromFile(String filePath,int pID,String title,String category, String[] tags) throws FileNotFoundException, IOException{

       FileReader fileReader = new FileReader(filePath);
       BufferedReader bufFileReader = new BufferedReader(fileReader);

       String wholeFile = "";
       String temp1 = "";
       while((temp1 = bufFileReader.readLine()) != null){
         wholeFile = wholeFile + temp1 + "\n";
       }

    /*   String[] paragraphs = AnalyzerUtil.getParagraphs(wholeFile, 0);
       Reader[] paraReaders = new StringReader[paragraphs.length];

       //create paragraphs readers
        for(int i=0; i<paragraphs.length; i++){
            paraReaders[i] = new StringReader(paragraphs[i]);
        }
       bufFileReader.close();
*/
         Post post = new Post();

         post.setpID(pID);
         post.setTitle(title);
         post.setDate("date");
         post.setBody(wholeFile);
         
         //post.setCategory(category);
         
         //post.setTags(tags);
         //post.setParaReaders(paraReaders);
         //post.setCatReader(new StringReader(post.getCategory()));

         //Reader[] tagsReaders = new StringReader[tags.length];
         //for(int i = 0; i<tags.length; i++){
           // tagsReaders[i] = new StringReader(tags[i]);
         //}
         //post.setTagsReaders(tagsReaders);

       return post;
     }
 }
